package com.xoa.service.easyReport.impl;

import com.xoa.dao.easyReport.RepFieldMapper;
import com.xoa.dao.easyReport.RepFlowMapper;
import com.xoa.dao.easyReport.RepTableMapper;
import com.xoa.model.easyReport.RepField;
import com.xoa.model.easyReport.RepFlow;
import com.xoa.model.easyReport.RepTable;
import com.xoa.model.users.Users;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.easyReport.RepTableService;
import com.xoa.service.users.UsersPrivService;
import com.xoa.service.users.UsersService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class RepTableServiceImpl implements RepTableService {

    @Resource
    RepTableMapper repTableMapper;

    @Resource
    RepFieldMapper repFieldMapper;

    @Autowired
    UsersService usersService;

    @Autowired
    UsersPrivService usersPrivService;

    @Autowired
    DepartmentService departmentService;

    @Resource
    RepFlowMapper repFlowMapper;

    @Override
    public ToJson<RepTable> insertRepTable(RepTable repTable) {
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        int i = repTableMapper.insertSelective(repTable);
        if(i>0){
            toJson.setFlag(0);
            toJson.setMsg("true");
        }
        return toJson;
    }

    @Override
    public ToJson<RepTable> updateRepTable(RepTable repTable) {
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        int i = repTableMapper.updateByPrimaryKeySelective(repTable);
        if(i>0){
            toJson.setFlag(0);
            toJson.setMsg("true");
        }
        return toJson;
    }

    //逻辑删除
    @Override
    public ToJson<RepTable> deleteRepTable(Integer repTableId) {
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        RepTable repTable = new RepTable();
        repTable.setRepTableId(repTableId);
        repTable.setStatus("3");
        int i = repTableMapper.updateByPrimaryKeySelective(repTable);
        if(i>0){
            toJson.setFlag(0);
            toJson.setMsg("true");
        }
        return toJson;
    }

    @Override
    public ToJson<RepTable> findRepTableById(Integer repTableId) {
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        try{
            RepTable repTable = repTableMapper.selectByPrimaryKey(repTableId);
            //查阅权限
            String ViewUserId = repTable.getViewUserId();
            if(!StringUtils.checkNull(ViewUserId)){
                String  userName= usersService.getUserNameById(ViewUserId);
                repTable.setViewUserName(userName+",");
            }
            String roleId = repTable.getViewPrivId();
            if(!StringUtils.checkNull(roleId)){
                String  roleName= usersPrivService.getPrivNameByPrivId(roleId,",");
                repTable.setViewPrivName(roleName+",");
            }
            String ViewDeptId = repTable.getViewDeptId();
            if(!StringUtils.checkNull(ViewDeptId)){
                String  depName= departmentService.getDeptNameByDeptId(ViewDeptId,",");
                if(!"ALL_DEPT".trim().equals(ViewDeptId)){
                    repTable.setViewDeptName(depName+",");
                }else{
                    repTable.setViewDeptName(depName);
                }
            }

            //管理权限
            String ManageUserId = repTable.getManageUserId();
            if(!StringUtils.checkNull(ManageUserId)){
                String  userName= usersService.getUserNameById(ManageUserId);
                repTable.setManageUserName(userName+",");
            }
            String ManagePrivId = repTable.getManagePrivId();
            if(!StringUtils.checkNull(ManagePrivId)){
                String  roleName= usersPrivService.getPrivNameByPrivId(ManagePrivId,",");
                repTable.setManagePrivName(roleName+",");
            }
            String ManageDeptId = repTable.getManageDeptId();
            if(!StringUtils.checkNull(ManageDeptId)){
                String  depName= departmentService.getDeptNameByDeptId(ManageDeptId,",");
                if(!"ALL_DEPT".trim().equals(ManageDeptId)){
                    repTable.setManageDeptName(depName+",");
                }else{
                    repTable.setManageDeptName(depName);
                }
            }
            toJson.setObject(repTable);
            toJson.setFlag(0);
            toJson.setMsg("true");
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<RepTable> findRepTable(HttpServletRequest request , RepTable repTable, String type) {
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        try {
            List<RepTable> repTables = new ArrayList<>();
            if(!StringUtils.checkNull(type) && "0".equals(type)){
                Map map = new HashMap();
                Date date = new Date();

                repTable.setStatus("1");
                repTable.setViewUserId(users.getUserId());
                repTable.setViewPrivId(users.getUserPriv()+"");
                repTable.setViewDeptId(users.getDeptId()+"");
                repTables = repTableMapper.selectCondition(repTable);
                Iterator<RepTable> iterator = repTables.iterator();
                while (iterator.hasNext()){
                    RepTable repTable1 = iterator.next();

                    //判断是否超时 ,,如果超时 并且在 没有当前用户数据  直接删除
                    if(repTable1.getClosingTime()!=null && repTable1.getClosingTime().getTime() < date.getTime()) {
                        map.put("tableId",repTable1.getRepTableId());
                        map.put("userId",users.getUserId());
                        List<RepFlow> repFlows = repFlowMapper.selectByTableIdTimeMap(map);
                        if (repFlows.isEmpty()){
                            iterator.remove();
                        }
                    }


                    if(repTable1.getClosingTime()!=null && repTable1.getClosingTime().getTime()< date.getTime()){
                        repTable1.setEditPerm(false);
                    }
                }
            }else if (!StringUtils.checkNull(type) && "1".equals(type)){
                repTable.setManageUserId(users.getUserId());
                repTable.setManagePrivId(users.getUserPriv()+"");
                repTable.setManageDeptId(users.getDeptId()+"");
                repTable.setStatus("1");
                repTables = repTableMapper.selectCondition(repTable);
                repTable.setStatus("2");
                repTables.addAll(repTableMapper.selectCondition(repTable));
            }else{
                repTables = repTableMapper.selectCondition(repTable);
            }
            toJson.setObj(repTables);
            toJson.setFlag(0);
            toJson.setMsg("true");
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<RepTable> cloneRepTable(Integer repTableId, String repTableName) {
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        try{
            RepTable repTable = repTableMapper.selectByPrimaryKey(repTableId);
            if(repTable != null){
                repTable.setRepTableId(null);
                repTable.setRepTableName(repTableName);
                //克隆报表
                int i = repTableMapper.insertSelective(repTable);
                if( i >0){
                    //克隆字段
                    List<RepField> repFieldList = repFieldMapper.selectByTableId(repTableId);
                    for(RepField repField :repFieldList){
                        repField.setFieldId(null);
                        repField.setRepTableId(repTable.getRepTableId());
                        repFieldMapper.insertSelective(repField);
                    }
                    toJson.setFlag(0);
                    toJson.setMsg("true");
                }
            }
        }catch (Exception e ){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson checkTableName(String tableName) {
        ToJson<RepTable> toJson = new ToJson<>(0,"true");
        try {
            List<RepTable> repTables = repTableMapper.selectByTableName(tableName);
            if(repTables.size()>0){
                toJson.setFlag(1);
                toJson.setMsg("err");
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return toJson;
    }
}
